import static org.junit.Assert.*;

import java.util.ArrayList;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.zanateh.wargame.game.*;
import org.zanateh.wargame.game.hexagon.BasicHexagon;
import org.zanateh.wargame.game.hexagon.HexField;
import org.zanateh.wargame.game.hexagon.Hexagon;
import org.zanateh.wargame.game.hexagon.HexField.NotInitializedException;
import org.zanateh.wargame.game.hexagon.TestHexagon;

public class HexFieldTest {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void test() {
		Assert.assertEquals(1, HexField.calculateColumnCount(10f, 3f));
	}
	
	@Test
	public void testZeroColumns() {
		Assert.assertEquals(0, HexField.calculateColumnCount(0f, 1f));
		Assert.assertEquals(0, HexField.calculateColumnCount(10f, 14f));
	}

	@Test
	public void testOneColumn() {
		Assert.assertEquals(1, HexField.calculateColumnCount(5f, 2.4f));
		Assert.assertEquals(1, HexField.calculateColumnCount(400f, 200f));
	}
	
	@Test
	public void testMultipleColumns() {
		Assert.assertEquals(3, HexField.calculateColumnCount(10f, 2f));
		Assert.assertEquals(8, HexField.calculateColumnCount(80f, 6f));
	}
	
	@Test
	public void testZeroRows() {
		Assert.assertEquals(0, HexField.calculateRowCount(10f, 9f));
	}	

	@Test
	public void testOneRows() {
		Assert.assertEquals(1, HexField.calculateRowCount(10f, 3f));
	}	

	@Test
	public void testMultipleRows() {
		Assert.assertEquals(2, HexField.calculateRowCount(15f, 3f));
	}
	
	@Test
	public void testHexfieldCreation() {
		HexField hf = new HexField(100, 100);
		hf.generateHexField(10);
		Assert.assertEquals(5, hf.getRowCount());
		Assert.assertEquals(5, hf.getRowCount());
	}
	
	@Test
	public void testHexfieldCreationFromStringArray() 
	{
		ArrayList<String> arrayList = new ArrayList<String>();
		arrayList.add("AAAAA");
		arrayList.add("AAAAA");
		arrayList.add("AAAAA");
		arrayList.add("AAAAA");
		arrayList.add("AAAAA");
		
		HexField hf = new HexField(100, 100);
		hf.loadHexMap(arrayList);
		Assert.assertEquals(5, hf.getRowCount());
		Assert.assertEquals(5, hf.getColumnCount());
	}
	
	@Test
	public void testHexAccess() throws NotInitializedException
	{
		HexField hf = new HexField(100,100);
		hf.generateHexField(10, 10);

		Hexagon hex = hf.getHex(4,5);
		Assert.assertNotNull(hex);
	}
	
	@Test(expected=IndexOutOfBoundsException.class)
	public void testHexColumnOutOfBounds() throws NotInitializedException
	{
		HexField hf = new HexField(100,100);
		hf.generateHexField(10, 10);
		
		Hexagon hex = hf.getHex(12, 3);
	}

	@Test(expected=IndexOutOfBoundsException.class)
	public void testHexRowOutOfBounds() throws NotInitializedException
	{
		HexField hf = new HexField(100,100);
		hf.generateHexField(10, 10);
		
		Hexagon hex = hf.getHex(3, 12);
	}
	
	@Test
	public void testHexfieldContentsFromStringArray() throws NotInitializedException
	{
		ArrayList<String> arrayList = new ArrayList<String>();
		arrayList.add("AT");
		arrayList.add("AT");

		HexField hf = new HexField(100, 100);
		hf.loadHexMap(arrayList);
		
		Assert.assertTrue("0,0 is a Basic", BasicHexagon.class.isInstance(hf.getHex(0, 0)));
		Assert.assertTrue("0,1 is a Test", TestHexagon.class.isInstance(hf.getHex(0, 1)));
		Assert.assertTrue("1,0 is a Basic", BasicHexagon.class.isInstance(hf.getHex(1, 0)));
		Assert.assertTrue("1,1 is a Test", TestHexagon.class.isInstance(hf.getHex(1, 1)));
	}
	

}
